package com.bingo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bingo.common.R;
import com.bingo.dao.WaterQualityMapper;
import com.bingo.entity.CurPool;
import com.bingo.entity.Immunization;
import com.bingo.entity.Medication;
import com.bingo.entity.WaterQuality;
import com.bingo.entity.dto.resp.ReportBasic;
import com.bingo.entity.dto.resp.WaterQualityRecord;
import com.bingo.service.CurPoolService;

import com.bingo.service.UserService;
import com.bingo.service.WaterQualityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import java.sql.Date;
import java.time.LocalDate;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
@Service
public class WaterQualityServiceImpl extends ServiceImpl<WaterQualityMapper, WaterQuality> implements WaterQualityService {
    private CurPoolService curPoolService;
    @Autowired
    public void setPrePoolService(CurPoolService curPoolService) {
        this.curPoolService = curPoolService;
    }

    private UserService userService;
    @Autowired
    public void setUserService(UserService userService) {
        this.userService = userService;
    }
    @Override
    // 新增水质记录
    public R add(WaterQuality waterQuality) {
        // 查询该塘
        CurPool pool = curPoolService.getCurPoolByNo(waterQuality.getPoolNo());

        if (pool == null) {
            return R.error("该塘不存在！");
        }
        if (pool.getType() == 0) {
            return R.error("该塘是空塘！");
        }
        // 查询是否已经存在当天水质记录
        QueryWrapper<WaterQuality> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("pool_no", waterQuality.getPoolNo());
        queryWrapper.eq("date",waterQuality.getDate());
        WaterQuality old = this.getOne(queryWrapper);

        // 获取当前日期
        LocalDate today = LocalDate.now();
        // 填充数据
        WaterQuality new_waterQuality = new WaterQuality();
        new_waterQuality.setUserId(waterQuality.getUserId());
        new_waterQuality.setPoolNo(waterQuality.getPoolNo());
        new_waterQuality.setTemperature(waterQuality.getTemperature());
        new_waterQuality.setPh(waterQuality.getPh());
        new_waterQuality.setConductivity(waterQuality.getConductivity());
        new_waterQuality.setOxygen(waterQuality.getOxygen());
        new_waterQuality.setTurbidity(waterQuality.getTurbidity());
        new_waterQuality.setLevel(waterQuality.getLevel());
        new_waterQuality.setDate(waterQuality.getDate());
        new_waterQuality.setReportDate(Date.valueOf(today));

        // 还未存在当天水质记录
        if (old == null) {
            // 保存
            this.save(new_waterQuality);
            return R.success("添加成功");
        } else {
            return R.error("存在当天记录！");
        }
    }

        @Override
        public List<WaterQualityRecord> selectWaterQualityRecord(List<String> poolNos, Date startTime, Date endTime, String userId) {
            if (poolNos == null || poolNos.size() == 0) {
                poolNos = curPoolService.getPoolNos();
            }

            QueryWrapper<WaterQuality> wrapper = new QueryWrapper<>();
            wrapper.in("pool_no", poolNos);
            wrapper.between("date", startTime, endTime);
            if (userId != null && !userId.equals("")) {
                wrapper.eq("user_id", userId);
            }
            List<WaterQuality> list = this.list(wrapper);
            LinkedList<WaterQualityRecord> waterQualityRecords = new LinkedList<>();
            HashMap<String, String> allUser = userService.getAllUser();

            for (WaterQuality waterQuality : list) {
                WaterQualityRecord waterQualityRecord = new WaterQualityRecord();
                ReportBasic reportBasic = new ReportBasic();
                reportBasic.setPoolNo(waterQuality.getPoolNo());
                String s = allUser.get(waterQuality.getUserId());
                reportBasic.setReporter(s);
                reportBasic.setDate(waterQuality.getDate());
                reportBasic.setReportDate(waterQuality.getReportDate());
                waterQualityRecord.setTemperature(waterQuality.getTemperature());
                waterQualityRecord.setPh(waterQuality.getPh());
                waterQualityRecord.setConductivity(waterQuality.getConductivity());
                waterQualityRecord.setOxygen(waterQuality.getOxygen());
                waterQualityRecord.setTurbidity(waterQuality.getTurbidity());
                waterQualityRecord.setLevel(waterQuality.getLevel());
                waterQualityRecord.setReportBasic(reportBasic);
                waterQualityRecords.add(waterQualityRecord);
            }
            return waterQualityRecords;
        }

        @Override
        public boolean deleteWaterQualityRecord(WaterQuality waterQuality) {
            if (waterQuality == null || waterQuality.getPoolNo() == null || waterQuality.getDate() == null) {
                return false;
            }
            QueryWrapper<WaterQuality> wrapper = new QueryWrapper<>();
            wrapper.eq("pool_no", waterQuality.getPoolNo());
            wrapper.eq("date", waterQuality.getDate());
            // 调用remove方法来删除记录
            return this.remove(wrapper);
        }
    @Override
    public boolean update(WaterQuality waterQuality) {
        // 创建更新条件
        UpdateWrapper<WaterQuality> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("pool_no", waterQuality.getPoolNo());
        updateWrapper.eq("date", waterQuality.getDate());

        // 设置需要更新的字段
        updateWrapper.set("temperature", waterQuality.getTemperature());
        updateWrapper.set("ph", waterQuality.getPh());
        updateWrapper.set("conductivity", waterQuality.getConductivity());
        updateWrapper.set("oxygen",waterQuality.getOxygen());
        updateWrapper.set("turbidity",waterQuality.getTurbidity());
        updateWrapper.set("level",waterQuality.getLevel());
        // 执行更新操作
        return this.update(updateWrapper);

    }
    }

